//
//  ReadDetailViewController.m
//  图片故事
//
//  Created by 刘鉴古 on 16/3/1.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "ReadDetailViewController.h"
#import "ReadTableViewCell.h"
#import "CreateManagerViewController.h"
#import <BmobSDK/Bmob.h>
#import "DetailedViewController.h"
#import "UIImageView+WebCache.h"
#import "UserInfoModal.h"
#import "Header.h"

//UMSocialUIDelegate
@interface ReadDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *detailTable;
@property (nonatomic,strong)NSMutableArray *cellArray;

#define Screen self.view.bounds.size
@end

@implementation ReadDetailViewController
-(UITableView *)detailTable{
    if (!_detailTable) {
        _detailTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height)];
        
        _detailTable.dataSource = self;
        _detailTable.delegate = self;
        
        [_detailTable setBackgroundColor:Bg_Color];
        
        _detailTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _detailTable.rowHeight = Screen.height - My_Height(46);
        
    }
    return _detailTable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _cellArray = [[NSMutableArray alloc] init];
    
    self.view.backgroundColor = Bg_Color;
    
    [self initNavigation];
    
    [self.view addSubview:self.detailTable];
    
}
- (void) initNavigation {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
    
    self.title = @"他/她的生活";
    
    // 设置导航控制器左右按钮
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor colorWithWhite:0.95 alpha:1]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"feather_write"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(action_changeModel)];
    
    self.navigationItem.rightBarButtonItem = rightItem;

}
-(void)action_changeModel{
    CreateManagerViewController *createStroyManger = [CreateManagerViewController shardManger];
    
    [self presentViewController:createStroyManger animated:YES completion:nil];
}
- (void) action_backItmeClickEvent {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadTableViewCell *cell = [[ReadTableViewCell alloc] init];

    cell.bmobObject = _selectObject;
    
    NSString *sql = [NSString stringWithFormat:@"select * from user where userName = '%@'",[_selectObject objectForKey:@"imageStoryUser"]];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        BmobQuery *query = [[BmobQuery alloc] init];
        
        [query queryInBackgroundWithBQL:sql block:^(BQLQueryResult *result, NSError *error) {
            if (!error){
                if (result.resultsAry.count > 0) {
                    
                    NSDictionary *dict1 = result.resultsAry[0];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{

                        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:[dict1 objectForKey:@"titleImage"]]];
                        
                        [cell.userHead sd_setImageWithURL:[NSURL URLWithString:[dict1 objectForKey:@"titleImage"]]];
                    });
                }
            }
        }];
    });
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UITapGestureRecognizer *detailTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_detailClickEvent)];
    
    [cell.detailImage addGestureRecognizer:detailTap];
    
    cell.detailImage.userInteractionEnabled = YES;
    cell.mapImage.image = [UIImage imageNamed:@"Map_2"];
    [cell.shareBtn addTarget:self action:@selector(action_shareClickEvent) forControlEvents:UIControlEventTouchUpInside];
    
    [cell setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
    [_cellArray addObject:cell];

    return cell;
}
- (void) action_detailClickEvent{
    DetailedViewController *detailedImage = [[DetailedViewController alloc] init];
    
    detailedImage.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
    detailedImage.bigImage = [[_cellArray objectAtIndex:0] detailImage].image;
    
    [self presentViewController:detailedImage animated:YES completion:nil];
}

- (void) action_shareClickEvent{
    /*
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"56dea03167e58efba8001091"
                                      shareText:@"通过ImgS处理的美图"
                                     shareImage:[[_cellArray objectAtIndex:0] detailImage].image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToRenren,UMShareToTencent,UMShareToDouban,nil]
                                       delegate:self];
     */
}
@end
